package com.yuhang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyCar;
import com.yuhang.entity.HyPet;
import com.yuhang.entity.HyUser;
import com.yuhang.service.HyCarService;
import com.yuhang.service.HyPetService;
import com.yuhang.service.HyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/hy-car")
public class HyCarController {

    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private HyCarService hyCarService;

    @RequestMapping("/showCar")
    public Map<String, Object> showCar(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                                       @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                       @RequestParam(value = "likename",defaultValue = "")String likename){
        IPage<HyCar> page = hyCarService.selectByPage(pageNum,pageSize,likename);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        return map;
    }
    @RequestMapping("/showUserCar")
    public Map<String, Object> showUserCar(HttpSession session,
                                           @RequestParam(value = "page",defaultValue = "1")int pageNum,
                                           @RequestParam(value = "limit",defaultValue = "5")int pageSize){
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        IPage<HyCar> page = hyCarService.selectByPage(pageNum,pageSize,hyUser.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        return map;
    }
    @RequestMapping("/addCar")
    public Map<String,Object> addCar(HyCar hyCar){
        hyCarService.save(hyCar);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/addUserCar")
    public Map<String,Object> addUserCar(HttpSession session,HyCar hyCar){
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        hyCar.setUserId(hyUser.getId());
        hyCar.setUserName(hyUser.getName());
        hyCarService.save(hyCar);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/deleteCar")
    public Map<String,Object> deleteCar(@NotEmpty Integer id){
        hyCarService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/updateCar")
    public Map<String,Object> updateCar(HyCar hyCar){
        hyCarService.updateById(hyCar);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }

}
